function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatArrTime(arr, key) {
  arr.forEach((item) => {
    item[key] = formatTime(new Date(item[key]));
  })
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getTimeBySec(sec, type) {
  var d = '';
  var h = parseInt(sec / 3600);
  var m = parseInt((sec - h * 3600) / 60);
  var s = sec - h * 3600 - m * 60;


  if (type == 'd') {
    d = parseInt(h / 24);
    h = h - d * 24;
  }

  return {
    str: [h, m, s].map(formatNumber).join(':'),
    d: d,
    h: formatNumber(h),
    m: formatNumber(m),
    s: formatNumber(s),
  }
}

function showAlert(msg, cb) {
  wx.showModal({
    //title: '提示',
    content: msg,
    showCancel: false,
    success: function(res) {
      if (res.confirm) {
        cb && cb();
      }
    }
  })
}

function showConfirm(msg, cb, cancelCb) {
  wx.showModal({
    //title: '提示',
    content: msg,
    showCancel: true,
    success: function(res) {
      if (res.confirm) {
        cb && cb();
      } else if (res.cancel) {
        cancelCb && cancelCb();
      }
    }
  });
}

function showOK(msg, cb) {
  wx.showToast({
    title: msg,
    icon: 'success',
    duration: 1500
  })
  setTimeout(() => {
    cb && cb();
  }, 1500)
}

function showWarn(msg, cb) {
  wx.showToast({
    title: msg,
    image: '/imgs/toast-warn.png',
    duration: 1500
  })
  setTimeout(() => {
    cb && cb();
  }, 1500)
}

function tips(msg, cb) {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 1500
  })
  setTimeout(() => {
    cb && cb();
  }, 1500)
}

function doLoading(tips) {
  wx.showLoading({
    title: tips || '加载中',
    mask: false,
  });
  // wx.showToast({
  //   title: '加载中',
  //   icon: 'loading',
  //   //duration: 1200
  // })
}

function hideLoading() {
  wx.hideLoading();
}

/**
 * 扫描二维码进入时，根据scene进行跳转
 * @param  {[type]}
 * @return {[type]}
 */
function jumpByScene(scene) {
  if (!scene) {
    return false;
  }

  scene = decodeURIComponent(scene);

  if (scene) {
    wx.redirectTo({
      url: scene
    })
    return true;
  }
  return false;
}

function parseOptions(options) {
  options = options || {};
  var scene = options.scene;
  var paramArr;

  if (scene) {
    scene = decodeURIComponent(scene);
    console.log(scene);
    paramArr = scene.split('&');
    paramArr.forEach(function(param) {
      var arr = param.split('=');

      if (arr.length == 2) {
        options[arr[0]] = arr[1];
      }
    });
  }
  return options;
  console.log(options);
}

//异步并行执行，统一处理结果
function async(arr, cb) {
  var len = arr.length;
  var num = 0;

  arr.forEach(function(func, index) {
    func && func(function(err, res) {
      num++;
      if (num == len) {
        cb && cb();
      }
    });
  });
}

function isPhone(str) {
  return /^1\d{10}$/.test(str.trim() || '');
}

function delHtmlTag(str) {
  return str.replace(/<[^>]+>/g, '');
}

function getLoction(cb) {
  wx.getLocation({
    type: 'wgs84',
    success(res) {
      const latitude = res.latitude
      const longitude = res.longitude
      const speed = res.speed
      const accuracy = res.accuracy
      wx.setStorageSync('location',true);
      cb && cb();
    },
    fail(res){
      console.log(res);
    }
  })
}
module.exports = {
  formatTime: formatTime,
  formatArrTime: formatArrTime,
  getTimeBySec: getTimeBySec,
  alert: showAlert,
  confirm: showConfirm,
  showOK: showOK,
  showOk: showOK,
  showWarn: showWarn,
  loading: doLoading,
  showLoading: doLoading,
  hideLoading: hideLoading,
  jumpByScene,
  parseOptions,
  async,
  isPhone,
  delHtmlTag,
  tips,
  getLoction
}